import Vue from 'vue'
import Router from 'vue-router'

Vue.use(Router)

const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err)
}

/* Layout */
import Layout from '@/layout'

export const baseRoutes = [
  {
    path: '/login',
    component: () => import('@/views/login/index'),
    hidden: true
  },
  {
    path: '/',
    component: Layout,
    redirect: 'dashboard',
    children: [
      {
        path: 'dashboard',
        name: 'Dashboard',
        component: () => import('@/views/dashboard/index'),
        meta: { title: '首页', icon: 'el-icon-house' }
      }
    ]
  },
  {
    path: '/order',
    component: Layout,
    meta: { title: '订单管理', icon: 'el-icon-postcard' },
    children: [
      {
        path: 'count1',
        name: 'count1',
        component: () => import('@/views/order/count1'),
        meta: { title: '每日销售额汇总' }
      },
            {
        path: 'count2',
        name: 'count2',
        component: () => import('@/views/order/count2'),
        meta: { title: '每日销售分布' }
      },
    ]
  },
  {
    path: '*',
    redirect: '/404',
    hidden: true,
    component: () => import('@/views/404'),
    children: [{ path: '/404', meta: { title: '404' } }]
  }
  // 404 page must be placed at the end !!!
]

const createRouter = () =>
  new Router({
    base: 'admin',
    mode: 'history', // require service support
    scrollBehavior: () => ({ y: 0 }),
    routes: baseRoutes
  })

const router = createRouter()

// Detail see: https://github.com/vuejs/vue-router/issues/1234#issuecomment-357941465
export function resetRouter() {
  const newRouter = createRouter()
  router.matcher = newRouter.matcher // reset router
}

export default router
